const path = require('path');
const multer = require('multer');

// Multer 配置
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, 'public/uploads/manure/');
  },
  filename: (req, file, cb) => {
    const ext = path.extname(file.originalname);
    const uniqueName = `${Date.now()}_${Math.random().toString(36).slice(2)}${ext}`;
    cb(null, uniqueName);
  }
});

const upload = multer({
  storage,
  limits: { fileSize: 5 * 1024 * 1024 },
  fileFilter: (req, file, cb) => {
    if (['image/jpeg', 'image/png'].includes(file.mimetype)) {
      cb(null, true);
    } else {
      cb(new Error('仅支持 JPG/PNG 格式'));
    }
  }
});

module.exports = { upload };